<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Course;

/**
 * CourseSearch represents the model behind the search form about `app\models\Course`.
 */
class CourseSearch extends Course
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'creadit', 'allperiod', 'studentnum', 'teachperiod', 'testperiod', 'machineperiod', 'otherperiod', 'weekperiod', 'updatetime', 'isfinish', 'addmanid', 'addtime', 'mon', 'tue', 'wed', 'thu', 'fri'], 'integer'],
            [['coursename', 'courseno', 'grade', 'profession', 'classtype1', 'classtype2', 'classes', 'checktype', 'teachweek', 'examineweek', 'teacher', 'siterequirement', 'arrangerequirement', 'mark', 'addman', 'regmatch', 'term'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Course::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            //分页
            'pagination' => [
                'pageSize' => 10,
            ],
            //排序
            'sort'=>[
                'defaultOrder'=>[
                    'id'=>SORT_ASC,
                ]
            ]
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'creadit' => $this->creadit,
            'allperiod' => $this->allperiod,
            'studentnum' => $this->studentnum,
            'teachperiod' => $this->teachperiod,
            'testperiod' => $this->testperiod,
            'machineperiod' => $this->machineperiod,
            'otherperiod' => $this->otherperiod,
            'weekperiod' => $this->weekperiod,
            'updatetime' => $this->updatetime,
            'isfinish' => $this->isfinish,
            'addmanid' => $this->addmanid,
            'addtime' => $this->addtime,
            'mon' => $this->mon,
            'tue' => $this->tue,
            'wed' => $this->wed,
            'thu' => $this->thu,
            'fri' => $this->fri,
        ]);

        $query->andFilterWhere(['like', 'coursename', $this->coursename])
            ->andFilterWhere(['like', 'courseno', $this->courseno])
            ->andFilterWhere(['like', 'grade', $this->grade])
            ->andFilterWhere(['like', 'profession', $this->profession])
            ->andFilterWhere(['like', 'classtype1', $this->classtype1])
            ->andFilterWhere(['like', 'classtype2', $this->classtype2])
            ->andFilterWhere(['like', 'classes', $this->classes])
            ->andFilterWhere(['like', 'checktype', $this->checktype])
            ->andFilterWhere(['like', 'teachweek', $this->teachweek])
            ->andFilterWhere(['like', 'examineweek', $this->examineweek])
            ->andFilterWhere(['like', 'teacher', $this->teacher])
            ->andFilterWhere(['like', 'siterequirement', $this->siterequirement])
            ->andFilterWhere(['like', 'arrangerequirement', $this->arrangerequirement])
            ->andFilterWhere(['like', 'mark', $this->mark])
            ->andFilterWhere(['like', 'addman', $this->addman])
            ->andFilterWhere(['like', 'regmatch', $this->regmatch])
            ->andFilterWhere(['like', 'term', $this->term]);

        return $dataProvider;
    }
}
